const http = require("http");
const fs = require("fs");

http.createServer(function (req, res) {
  // http://127.0.0.1:3000/login.html
  // http://127.0.0.1:3000/index.html
  // 1. 获取地址
  let pathname = req.url;
  pathname=pathname=='/'?'/index.html':pathname;
  if(pathname !== "/favicon.ico") {
    // 通过 fs 模块读取文件
    fs.readFile(`./static/${pathname}`, (err, data) => {
      if(err) {
        res.writeHead(404, {'Content-Type': 'text/html;charset="utf-8"'});
        res.end("404这个页面不存在");
      }

      res.writeHead(404, {'Content-Type': 'text/html;charset="utf-8"'});
      res.end(data);
    })
  }
}).listen(3000);

console.log("Server running at http://127.0.0.1:3000/");